local shangshu = fk.CreateSkill {
  name = "lingling__shangshu",
}

Fk:loadTranslationTable{
  ["lingling__shangshu"] = "上书",
  [":lingling__shangshu"] = "出牌阶段限一次，你可以与一名其他角色各随机获得一张锦囊牌，然后其使用的下一张不为以此法获得的非转化的锦囊牌无效。",

  ["#lingling__shangshu"] = "上书：与一名角色各获得一张锦囊牌",
  ["@@lingling__shangshu"] = "上书",
}

shangshu:addEffect("active", {
  anim_type = "control",
  prompt = "#lingling__shangshu",
  card_num = 0,
  target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(shangshu.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0 and to_select ~= player
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local card = room:getCardsFromPileByRule(".|.|.|.|.|trick", 1, "allPiles")
    if #card > 0 then
      room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonJustMove, shangshu.name, nil, false, player)
    end
    if not target.dead then
      card = room:getCardsFromPileByRule(".|.|.|.|.|trick", 1, "allPiles")
      if #card > 0 then
        room:moveCardTo(card, Card.PlayerHand, target, fk.ReasonJustMove, shangshu.name, nil, false, player)
        if not target.dead then
          room:addTableMark(target, "@@lingling__shangshu", card[1])
        end
      end
    end
  end,
})

shangshu:addEffect(fk.CardUsing, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and player:getMark("@@lingling__shangshu") ~= 0 and
      data.card.type == Card.TypeTrick and not data.card:isVirtual()
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if not table.contains(player:getTableMark("@@lingling__shangshu"), data.card.id) then
      data.toCard = nil
      data:removeAllTargets()
    end
    room:setPlayerMark(target, "@@lingling__shangshu", 0)
  end,
})

return shangshu
